unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, 
  dxBar, dxRibbon, dxRibbonForm, dxRibbonSkins, cxGraphics, cxControls, cxLookAndFeels,
  cxLookAndFeelPainters, cxClasses, dxRibbonBackstageView, dxSkinsCore,
  dxSkinBlue, dxSkinsdxRibbonPainter,
  dxSkinsdxBarPainter, cxStyles, dxSkinscxPCPainter, cxCustomData, cxFilter,
  cxData, cxDataStorage, cxEdit, cxNavigator, cxGridCustomTableView,
  cxGridTableView, cxGridCustomView, cxGridLevel, cxGrid, dxStatusBar,
  dxRibbonStatusBar, Vcl.StdCtrls, Vcl.ComCtrls, cxProgressBar, cxBarEditItem,
  dxBarExtItems,chimera.json, cxMemo, cxContainer, cxLabel,ShellApi;

type
  TForm2 = class(TdxRibbonForm)
    dxBarManager1: TdxBarManager;
    dxBarManager1Bar1: TdxBar;
    dxRibbon1: TdxRibbon;
    dxRibbon1Tab1: TdxRibbonTab;
    dxRibbonBackstageView1: TdxRibbonBackstageView;
    dxRibbonBackstageViewTabSheet1: TdxRibbonBackstageViewTabSheet;
    dxRibbonStatusBar1: TdxRibbonStatusBar;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    MainData: TcxGridTableView;
    MainDataColumn1: TcxGridColumn;
    dxBarManager1Bar2: TdxBar;
    dxBarLargeButton1: TdxBarLargeButton;
    dxBarLargeButton2: TdxBarLargeButton;
    MainDataColumn2: TcxGridColumn;
    dxBarManager1Bar3: TdxBar;
    Progress: TcxBarEditItem;
    Progres: TdxBarProgressItem;
    MainDataColumn3: TcxGridColumn;
    MainDataColumn4: TcxGridColumn;
    MainDataColumn5: TcxGridColumn;
    MainDataColumn6: TcxGridColumn;
    MainDataColumn7: TcxGridColumn;
    NumberCol: TcxGridColumn;
    MainDataColumn9: TcxGridColumn;
    MainDataColumn10: TcxGridColumn;
    MainDataColumn11: TcxGridColumn;
    MainDataColumn12: TcxGridColumn;
    MainDataColumn13: TcxGridColumn;
    MainDataColumn14: TcxGridColumn;
    MainDataColumn15: TcxGridColumn;
    MainDataColumn16: TcxGridColumn;
    MainDataColumn17: TcxGridColumn;
    MainDataColumn18: TcxGridColumn;
    MainDataColumn19: TcxGridColumn;
    MainDataColumn20: TcxGridColumn;
    MainDataColumn21: TcxGridColumn;
    ArrayText: TdxBarEdit;
    dxBarButton1: TdxBarButton;
    MemoJSON: TcxBarEditItem;
    cxLabel1: TcxLabel;
    cxLabel2: TcxLabel;
    dxBarLargeButton3: TdxBarLargeButton;
    SaveDlg: TFileSaveDialog;
    dxBarLargeButton4: TdxBarLargeButton;
    dxBarSpinEdit1: TdxBarSpinEdit;
    procedure dxBarLargeButton1Click(Sender: TObject);
    procedure dxBarLargeButton3Click(Sender: TObject);
    procedure MainDataCellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure dxBarLargeButton4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    function GetJSONData(FileName : string) : string;
    procedure GetValueFromJSON(JSONTXT,FileName : string; Number : integer);
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.dxBarLargeButton1Click(Sender: TObject);
Var
  Path    : String;
  SR      : TSearchRec;
  DirList : TStrings;
  I: Integer;
  JSON : string;
begin
MainData.BeginUpdate();
    Path:='C:\Games\World_of_Tanks\replays\'; //Get the path of the selected file
    DirList:=TStringList.Create;
    try
      if FindFirst(Path+'*.*', faArchive, SR) = 0 then
      begin
        repeat
            if SR.Name <> Path+'temp1.wotreplay' then
            DirList.Add(SR.Name); //Fill the list
        until FindNext(SR) <> 0;
        FindClose(SR);
      end;
      //MainData.DataController.RecordCount := DirList.Count;
      Progres.Min := 0;
      Progres.Max := DirList.Count-1;

      for I := 0 to DirList.Count-1 do
      begin
        Caption := 'File Name: '+DirList.Strings[i]+' File '+IntToStr(i)+' of '+IntToStr(DirList.Count-1);
        Application.ProcessMessages;
        //MainData.DataController.SetValue(i,MainDataColumn1.Index,DirList.Strings[i]);
        JSON := GetJSONData(Path+DirList.Strings[i]);
        //MainData.DataController.SetValue(i,MainDataColumn2.Index,JSON);
        GetValueFromJSON(JSON,DirList.Strings[i],i);
        Progres.Position := I;
      end;


    finally
     DirList.Free;
    end;
MainData.EndUpdate;
end;



procedure TForm2.dxBarLargeButton3Click(Sender: TObject);
var
  FilePath,CopyPath: string;
begin
  if SaveDlg.Execute then
  begin
  FilePath := MainDataColumn1.EditValue;
  CopyPath := SaveDlg.FileName;
  CopyFile(PChar(FilePath),PChar(CopyPath),False);
 // ShellExecute(Handle, 'open', PChar(FilePath), nil, nil, SW_SHOWNORMAL) ;
  end;
end;

procedure TForm2.dxBarLargeButton4Click(Sender: TObject);
  var
  Filename : String;
begin
FileName := MainDataColumn1.EditValue;
ShellExecute(
  Application.Handle,
  'open',
  'C:\Games\World_of_Tanks\WorldOfTanks.exe',
  PChar(FileName),
  nil,
  SW_SHOWNORMAL
);
end;

function TForm2.GetJSONData(FileName: string) : string;
var
  JSONData : TRichEdit;
  JSONString,LineText : String;
  j,LastBracket: Integer;
  ReplayFile : TextFile;
  I: Integer;
  x: Integer;

begin
  try
    AssignFile(ReplayFile,FileName);
    FileMode := fmOpenRead;
    Reset(ReplayFile);
    j := 0;
    while not Eof(ReplayFile) do
    begin
    if J > 30 then // infinite cycle fail safe
    begin
      break;
    end;
      Read(ReplayFile,LineText);
      if Pos('{',LineText) > 0 then
      begin
        JSonString :=  LineText;
        JSONString := Copy(LineText,Pos('{',LineText),(LastDelimiter('}',LineText)-Pos('{',LineText)+1));  //length(LineText));
        for x := 126 to 255 do
        begin
          JSONString := StringReplace(JSONString,Chr(x),' ',[rfReplaceAll,rfIgnoreCase]);
        end;
        //ShowMessage(JSONString);
        //StrRScan()
        //LastBracket := 0;
//        for I := Length(JSONString) downto 0 do
//        begin
//          if ((JSONString[i] = '}') AND (LastBracket = 0)) then
//          begin
//            LastBracket := i;
//            Continue;
//          end;
//        end;
//        JSONString := Copy(JSONString,0,LastBracket);
        break;
      end;
        J := J+1;
    end;

  finally
    CloseFile(ReplayFile);
  end;
  result := JSONString;
end;

procedure TForm2.GetValueFromJSON(JSONTXT,FileName: string; Number : integer);
var
  i : integer;
  obj : IJSONObject;
  J: Integer;
begin
  obj := JSON(JSONTXT);
  try

  for i := 0 to obj.Objects['vehicles'].count-1 do
  begin
    MainData.DataController.RecordCount := MainData.DataController.RecordCount+1;
    MainData.DataController.SetValue(MainData.DataController.RecordCount-1,NumberCol.Index,IntToStr(Number));
    MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn1.Index,'C:\Games\World_of_Tanks\replays\'+FileName);
    MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn2.Index,JSONTXT);
    //MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn3.Index,obj.Objects['vehicles'].Names[i]);
    MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn3.Index,obj.Strings['clientVersionFromXml']);
    MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn4.Index,obj.Strings['clientVersionFromExe']);
    MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn5.Index,obj.Strings['mapDisplayName']);
    MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn6.Index,obj.Strings['gameplayID']);
    MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn7.Index,IntToStr(obj.Integers['playerID']));

    //Vehiacles Data
    //for J := 0 to obj.Objects['vehicles'].Objects[obj.Objects['vehicles'].Names[i]].Count-1   do
    //begin
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn9.Index,obj.Objects['vehicles'].Names[i]);
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn10.Index,obj.Objects['vehicles'].Objects[obj.Objects['vehicles'].Names[i]].Strings['vehicleType']);
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn11.Index,BoolToStr(obj.Objects['vehicles'].Objects[obj.Objects['vehicles'].Names[i]].Booleans['isAlive']));
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn12.Index,obj.Objects['vehicles'].Objects[obj.Objects['vehicles'].Names[i]].Strings['name']);
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn13.Index,obj.Objects['vehicles'].Objects[obj.Objects['vehicles'].Names[i]].Integers['igrType']);
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn14.Index,obj.Objects['vehicles'].Objects[obj.Objects['vehicles'].Names[i]].Strings['clanAbbrev']);
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn15.Index,obj.Objects['vehicles'].Objects[obj.Objects['vehicles'].Names[i]].Integers['team']);
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn16.Index,BoolToStr(obj.Objects['vehicles'].Objects[obj.Objects['vehicles'].Names[i]].Booleans['isTeamKiller']));
    //end;
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn17.Index,obj.Strings['dateTime']);
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn18.Index,obj.Strings['mapName']);
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn19.Index,obj.Strings['playerName']);
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn20.Index,IntToStr(obj.Integers['battleType']));
      MainData.DataController.SetValue(MainData.DataController.RecordCount-1,MainDataColumn21.Index,obj.Strings['playerVehicle']);
  end;
EXCEPT

end;


end;

procedure TForm2.MainDataCellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
  var
  Filename : String;
begin
FileName := MainDataColumn1.EditValue;
ShellExecute(
  Application.Handle,
  'open',
  'C:\Games\World_of_Tanks\WorldOfTanks.exe',
  PChar(FileName),
  nil,
  SW_SHOWNORMAL
);
end;

end.
